#include "itemwidget.h"
#include "ui_itemwidget.h"
#include "personal_space.h"
#include "QMenu"
#include "QDesktopServices"
#include "QUrl"
#include "QNetworkRequest"
#include "QNetworkReply"
#include "video.h"
#include "QJsonParseError"
#include "QJsonArray"
#include "QJsonDocument"
#include "QJsonObject"
#include "QJsonValueRef"
#include <QDebug>

static Personal_Space * ptrParent;

ItemWidget::ItemWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ItemWidget)
{
    ui->setupUi(this);
    ptrParent = static_cast<Personal_Space *>(this->parentWidget());
}

ItemWidget::~ItemWidget()
{
    delete ui;
}

//点击播放视频槽函数
void ItemWidget::media(QString vpath)
{
    //QString local_path = vpath; //a.txt、a.exe、a.mp3、a.mp4、a.rmvb等
    QString path = QString("file:///") + vpath;
    QDesktopServices::openUrl(QUrl(path));
}

void ItemWidget::dealMsg(QNetworkReply *reply)
{
    QByteArray array = reply->readAll();
    QString s = array;
    if(QString::compare(s,"视频修改成功！") == 0)
    {
        QString reName = ui->lineEdit_info->text();
        emit rename(videoid, reName);
    }
}

//重命名的槽函数
void ItemWidget::saveEditinfo(int videoid)
{  
    QString vname;
    vname = ui->lineEdit_info->text();

    // URL
    QString baseUrl = "http://127.0.0.1:5000/VideoUpdate?Videoid=" + QString::number(videoid) + "&Videoname=" + vname;
    QUrl url(baseUrl);
    qDebug() << baseUrl;

    // 构造请求QNetworkRequest
    QNetworkRequest request;
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
    request.setUrl(url);

    QNetworkAccessManager *manager = new QNetworkAccessManager();

    // 发送get请求
    manager->get(request);
    ui->lineEdit_info->setEnabled(false);

    connect(manager, SIGNAL(finished(QNetworkReply*)), this,SLOT(dealMsg(QNetworkReply*)));
}

void ItemWidget::ReName(int videoid)
{
    //设置文本框可编辑
    ui->lineEdit_info->setEnabled(true);
    //连接按回车后获取文本框内容槽函数
    connect(ui->lineEdit_info,&QLineEdit::returnPressed,this,[=](){saveEditinfo(videoid);});
}

//删除的槽函数
void ItemWidget::Delete(int videoid)
{
    // URL
    QString baseUrl = "http://127.0.0.1:5000/VideoDelete?Videoid=" + QString::number(videoid);
    qDebug() << baseUrl;
    QUrl url(baseUrl);

    // 构造请求QNetworkRequest
    QNetworkRequest request;
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
    request.setUrl(url);

    QNetworkAccessManager *manager = new QNetworkAccessManager();

    // 发送get请求
    manager->get(request);

    connect(manager, &QNetworkAccessManager::finished, [=](QNetworkReply* reply){
        QByteArray array = reply->readAll();
        QString s = array;
        if(s.compare("视频删除成功") == 0)
        {
            emit remake();
        }
    });
}

//设置基本数据函数
void ItemWidget::SetData(const int &qstrid, const QString& qstrFileName,  const QString& qstrPic, const QString& qpath)
{
    //保存视频id
    videoid = qstrid;
    //保存视频地址
    videopath = qpath;

    this->qstrFileName = qstrFileName;
    this->qstrPic = qstrPic;

    //文本框显示文件名
    ui->lineEdit_info->setText(qstrFileName);
    ui->lineEdit_info->setEnabled(false);

    //显示视频封面或图片
    QPixmap pixmapPic(qstrPic);
    int iWidth = ui->label_vedio->width();
    int iHeight = ui->label_vedio->height();
    QPixmap pixmapPicFit = pixmapPic.scaled(iWidth, iHeight, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    ui->label_vedio->setPixmap(pixmapPicFit);

    //设置按钮菜单栏
    QMenu *m = new QMenu(this);
    m->setStyleSheet("QMenu{background-color:rgb(255,255,255)}QMenu::item:selected{background-color:blue}");
    QAction* pActionRename = m->addAction("重命名");
    QAction* pActionDel = m->addAction("删除");

    //消除“未引用的局部变量”警告
    Q_UNUSED(pActionRename)
    //Q_UNUSED(pActionLoad)
    Q_UNUSED(pActionDel)

    //设置按钮
    ui->pushButton_menu->setMenu(m);
    ui->pushButton_menu->setStyleSheet("QPushButton::menu-indicator{image:None;}");

    //连接点击删除视频槽函数
    connect(pActionDel,&QAction::triggered,this,[=](){Delete(videoid);});
    //连接点击重命名视频槽函数
    connect(pActionRename,&QAction::triggered,this,[=](){ReName(videoid);});
    //连接点击播放视频槽函数
    connect(ui->label_vedio,&MyClickLabel::clicked,this,[=](){media(videopath);});
}
